package com.mlr.mlrcturebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlr.mlrcturebackend.common.BaseResponse;
import com.mlr.mlrcturebackend.common.ResultUtils;
import com.mlr.mlrcturebackend.exception.BusinessException;
import com.mlr.mlrcturebackend.exception.ErrorCode;
import com.mlr.mlrcturebackend.model.dto.tag.TagAddRequest;
import com.mlr.mlrcturebackend.model.dto.tag.TagQueryRequest;
import com.mlr.mlrcturebackend.model.dto.tag.TagUpdateRequest;
import com.mlr.mlrcturebackend.model.entity.Tag;
import com.mlr.mlrcturebackend.model.vo.TagVO;
import com.mlr.mlrcturebackend.service.TagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 标签接口
 */
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;

    /**
     * 创建标签
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTag(@RequestBody TagAddRequest tagAddRequest) {
        if (tagAddRequest == null || tagAddRequest.getTagName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag tag = new Tag();
        tag.setTagName(tagAddRequest.getTagName());
        tag.setDescription(tagAddRequest.getDescription());
        tagService.validTag(tag, true);
        boolean result = tagService.save(tag);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(tag.getId());
    }

    /**
     * 删除标签
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTag(@RequestBody Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = tagService.removeById(id);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     * 更新标签
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTag(@RequestBody TagUpdateRequest tagUpdateRequest) {
        if (tagUpdateRequest == null || tagUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag tag = new Tag();
        tag.setId(tagUpdateRequest.getId());
        tag.setTagName(tagUpdateRequest.getTagName());
        tag.setDescription(tagUpdateRequest.getDescription());
        tagService.validTag(tag, false);
        boolean result = tagService.updateById(tag);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取标签
     */
    @GetMapping("/get")
    public BaseResponse<Tag> getTagById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag tag = tagService.getById(id);
        if (tag == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(tag);
    }

    /**
     * 分页获取标签列表
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<TagVO>> listTagByPage(@RequestBody TagQueryRequest tagQueryRequest) {
        long current = tagQueryRequest.getCurrent();
        long size = tagQueryRequest.getPageSize();
        Page<Tag> tagPage = tagService.page(new Page<>(current, size),
                tagService.getQueryWrapper(tagQueryRequest));
        // 转换为VO分页对象
        Page<TagVO> tagVOPage = new Page<>(current, size, tagPage.getTotal());
        List<TagVO> tagVOList = tagService.getTagVOList(tagPage.getRecords());
        tagVOPage.setRecords(tagVOList);
        return ResultUtils.success(tagVOPage);
    }

    /**
     * 获取所有标签列表
     */
    @GetMapping("/list/all")
    public BaseResponse<List<TagVO>> listAllTags() {
        List<Tag> tagList = tagService.list(tagService.getQueryWrapper(new TagQueryRequest()));
        List<TagVO> tagVOList = tagService.getTagVOList(tagList);
        return ResultUtils.success(tagVOList);
    }
}